package com.hznu.jspFinal.controller;

import com.hznu.jspFinal.entity.Composepaper;
import com.hznu.jspFinal.entity.Exam;
import com.hznu.jspFinal.myBeans.Exercise;
import com.hznu.jspFinal.myBeans.R;
import com.hznu.jspFinal.service.ComposePaperService;
import com.hznu.jspFinal.service.ExamService;
import com.hznu.jspFinal.service.ExerciseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Api(description = "考试管理接口",value = "Exam")
@RestController
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    private ExamService examService;
    @Autowired
    private ComposePaperService composePaperService;
    @Autowired
    private ExerciseService exerciseService;

    /**
     * author:wp
     * time:22:49 2018/06/17
     * @param exam
     * @return
     */
    @ApiOperation(value = "创建一场课程班的考试")
    @PostMapping("/add")
    public R add(@RequestBody Exam exam){
        try{
            examService.add(exam);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "删除考试")
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable Integer id){
        try{
            examService.delete(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "查找考试")
    @GetMapping("/findByExamId/{id}")
    @ResponseBody
    public R findByExamId(@PathVariable Integer id){
        try{
            return R.ok().put("data",examService.findByExamId(id));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }


    @ApiOperation(value = "更新考试")
    @PostMapping("/updateByExamId")
    public R updateByExamId(@RequestBody Exam exam){
        try{
            examService.update(exam);
            return R.ok("修改考试成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("修改考试失败");
        }
    }

    @ApiOperation(value="学生查找自己的考试")
    @GetMapping("/findByStudentId/{studentid}")
    public R findByStudentId(@PathVariable Integer studentid){
        try{
            return R.ok().put("data",examService.findByStudentId(studentid));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找某门课程的考试")
    @GetMapping("/findByCourseId/{courseid}")
    public R findByCourseId(@PathVariable Integer courseid){
        try{
            return R.ok().put("data",examService.selectByCourseId(courseid));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查找教学班的考试")
    @GetMapping("/findByCourseTeachId/{courseteachid}")
    public R findByCourseTeachId(@PathVariable Integer courseteachid){
        try{
            return R.ok().put("data",examService.selectByCourseTeachid(courseteachid));
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @ApiOperation(value = "查看考试的详细内容")
    @GetMapping("/findExerciseDetails/{examid}")
    public R findExerciseDetails(@PathVariable Integer examid){
        try{
            //找到考试的详细内容
            Exam exam = examService.findByExamId(examid);
            Integer examPaperId = exam.getExampaperid();
            List<Composepaper> composepapers = composePaperService.selectByPaperId(examPaperId);
            List<Exercise> exercises = new LinkedList<>();
            for (Composepaper composepaper : composepapers){
                Exercise exercise = exerciseService.findByExerinfoid(composepaper.getExerinfoid());
                exercises.add(exercise);
            }
            return R.ok().put("data",exercises);
        }catch (Exception e){
            e.printStackTrace();
            return R.error("获取考试详细信息失败");
        }

    }


}
